package com.eu.ebpm.engine.init;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eu.ebpm.engine.entity.args.Param;
import com.eu.ebpm.engine.entity.deploy.Deploy;
import com.eu.ebpm.engine.entity.deploy.Mission;
import com.eu.ebpm.engine.entity.deploy.Transfer;
import com.eu.ebpm.engine.express.Express;
import com.eu.ebpm.engine.pvm.EngineServiceType;
import com.eu.ebpm.engine.repository.DeployRepository;
import com.eu.ebpm.engine.repository.MissionRepository;
import com.eu.ebpm.engine.repository.TransferRepository;
import org.springframework.util.StringUtils;

@Service
public class EbpmInitialization {

	@Autowired
	private DeployRepository deployRepository;

	@Autowired
	private MissionRepository missionRepository;

	@Autowired
	private TransferRepository transferRepository;

	@PostConstruct
	public void init() {
		
		EbpmCache.clearAll();
		
		// 1、初始化部署信息
		List<Deploy> deploys = initDeployList();

		// 2、将 deploys、missions、transfers 写入缓存
		for (Deploy deploy : deploys) {
			EbpmCache.getKeyDeploys().put(deploy.getKey(), deploy);
			EbpmCache.getDeploys().put(deploy.getId(), deploy);
			for (Mission mission : deploy.getMissions()) {
				EbpmCache.getMissions().put(mission.getId(), mission);
				for (Transfer transfer : mission.getTransfers()) {
					String key = transfer.getFromMissionId().toString() + "_" + transfer.getToMissionId().toString();
					EbpmCache.getTransfers().put(key, transfer);
				}
			}
		}

		// 3、设置toTransfers、toMission
		for (Transfer t : EbpmCache.getTransfers().values()) {
			Mission mission = EbpmCache.getMissions().get(t.getToMissionId());
			if (mission.getToTransfers() == null) {
				mission.setToTransfers(new ArrayList<Transfer>());
			}
			mission.addToTransfer(t);
			t.setToMission(mission);
		}

	}

	/**
	 * 初始化部署流程信息
	 * 
	 * @return
	 */
	public List<Deploy> initDeployList() {

		List<Deploy> deploys = deployRepository.list(Param.map("version", 0));

		for (Deploy d : deploys) {

			d.setMissions(initMissionList(d.getId()));

			for (Mission m : d.getMissions()) {
				if (m.getType() == EngineServiceType.START_EVENT) {
					d.setStartMission(m);
				}
				m.setDeploy(d);
			}

		}

		return deploys;
	}

	public List<Mission> initMissionList(long deployId) {

		List<Mission> missions = missionRepository.queryList(Param.map("deployId", deployId));

		for (Mission m : missions) {
			m.setTransfers(initTransferList(m.getId()));
			for (Transfer t : m.getTransfers()) {
				t.setFromMission(m);
			}
		}

		return missions;
	}

	public List<Transfer> initTransferList(long missionId) {

		List<Transfer> transfers = transferRepository.queryTransferList(Param.map("fromMissionId", missionId));

		for (Transfer t : transfers) {
			t.setToMission(new Mission());
			t.getToMission().setId(t.getToMissionId());
			if (!StringUtils.isEmpty(t.getExpress())) {
				t.setExpressCondition(new Express(t.getExpress()));
			}
		}

		return transfers;
	}

}
